// pages/inde/inde.js
var WXGrid = require('../../utils/wxgrid.js')
var bibleData = require('../../utils/bible.js')
var glo = require('../../utils/global.js')
var config = require('../../utils/config.js')

var app = getApp();
var lastTapDiffTime = 0;
var bibleMsg = {
    bookId: '',
    bookName: '',
    chapterCount: '',
    chapterId: '',
    verseId: ''

};


Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageEnable2: true,

    },

    loadPersonal: function() {
        var that = this;
        // that.setData({ currentTab: 1 });
        wx.getSetting({
            success: function(res) {
                if (res.authSetting['scope.userInfo'] || 1 == 1) { //改
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                    app.getUserInfo(function(userInfo) {
                        var bibleRead = glo.punchCalcLastYear(userInfo.punch);
                        wx.setStorageSync('bibleRead', bibleRead);
                        /* for (var i = 0; i < 66; i++) {
                           if (i < 39) {
                             if (bibleRead[i][0] == -1) {
                               bibleData.oldbooks[i].flag = "c";
                               bibleData.oldbooks[i].readCount = 0;
                             } else if (bibleRead[i][0] == 0) {
                               bibleData.oldbooks[i].flag = "b";
                               bibleData.oldbooks[i].readCount = 0;
                             } else {
                               bibleData.oldbooks[i].flag = "a";
                               bibleData.oldbooks[i].readCount = bibleRead[i][0];
                             }
                           } else {
                             if (bibleRead[i][0] == -1) {
                               bibleData.newbooks[i - 39].flag = "c";
                               bibleData.newbooks[i - 39].readCount = 0;
                             } else if (bibleRead[i][0] == 0) {
                               bibleData.newbooks[i - 39].flag = "b";
                               bibleData.newbooks[i - 39].readCount = 0;
                             } else {
                               bibleData.newbooks[i - 39].flag = "a";
                               bibleData.newbooks[i - 39].readCount = bibleRead[i][0];
                             }
                           }
                         }*/

                        // wx.request({
                        //   url: glo.https + '/person/getmsg?userid=' + userInfo.userId,
                        //   method: 'GET',
                        //   header: {
                        //     'Content-Type': 'application/json'
                        //   },
                        //   success: function(res) {

                        //     var msgs = res.data;
                        //     for (var i = 0; i < msgs.length; i++) {

                        //       var msg = msgs[i];
                        //       wx.navigateTo({
                        //         url: '../notice/notice' + '?nickname=' + userInfo.nickName + '&msg=' + msg.content,
                        //         success: function(res) {
                        //           wx.hideLoading();
                        //           // success
                        //         },
                        //         fail: function() {
                        //           wx.hideLoading();
                        //           wx.showToast({
                        //             title: '加载失败，请检查网络',
                        //             icon: 'loading',
                        //             duration: 1000
                        //           })
                        //           // fail
                        //         }
                        //       })
                        //       // wx.showToast({
                        //       //   title: msg.content,
                        //       //   icon: 'loading',
                        //       //   duration: 2000
                        //       // })

                        //     }

                        //   }
                        // })

                    });
                } else {
                    // wx.navigateTo({
                    //   url: '../authorize/authorize',
                    // })
                }
            }
        });
    },

    loadBibleV: function(ff) {
        var that = this;
        wx.request({
            url: glo.https + '/conf/getval?mkey=' + glo.enablev,
            method: 'GET',
            success: function(res0) {
                var enablev = res0.data;
                wx.setStorage({
                    key: glo.enablev,
                    data: enablev,
                    success: function() {
                        bibleData.reLoadBible();
                        ff();
                    }
                })

                //  wx.setStorageSync('enablev', enablev);

            },
            fail: function() {
                wx.setStorageSync(glo.enablev, '0');
                bibleData.reLoadBible();
                wx.hideLoading();
                ff();
            }

        })
    },

    loadCatalog: function() {
        console.log('fffffffffffffff')
        var that = this;
        // that.setData({ currentTab: 1 });
        var wxgrid1 = new WXGrid;
        wxgrid1.init(13, 3);
        var wxgrid2 = new WXGrid;
        wxgrid2.init(9, 3);
        if (bibleData.oldbooks == undefined) {
            return;
        }
        wxgrid1.data.add("classifies", bibleData.oldbooks);
        wxgrid2.data.add("classifies", bibleData.newbooks);
        console.log(wxgrid1)
        console.log(wxgrid2)
        var res = wx.getSystemInfoSync();
        var panelHeight = res.windowHeight - res.windowWidth / 750 * 80
        console.debug("h px" + res.windowWidth / 750 * 100);
        if (res.platform == "android") {
            if (res.windowWidth >= 360) {
                panelHeight = 462;
            } else {
                panelHeight = 396;
            }
        }
        that.setData({
            wxgrid: wxgrid1,
            wxgridNew: wxgrid2,
            winHeight: panelHeight
        });
        that.loadPersonal();
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        wx.getStorage({
            key: 'lastread',
            success: function(res) {
                console.log(res)
            }
        });

        var that = this;
        that.setData({
            currentTab: 1
        });


        var preloadFun = function() {
            if (glo.validBible()) {
                var v = "%E5%9C%A3%E7%BB%8F"
                var o = "%E6%97%A7%E7%BA%A6"
                var n = "%E6%96%B0%E7%BA%A6"
                var k = "%E8%BE%93%E5%85%A5%E5%9C%A3%E7%BB%8F%E7%BB%8F%E6%96%87"
                wx.setNavigationBarTitle({
                    title: config.btitle, //decodeURI(v),
                })
                that.setData({
                    oldMenu: decodeURI(o),
                    newMenu: decodeURI(n),
                    searchTip: decodeURI(k),
                    pageEnable: true,
                    pageEnable1: true,
                    pageEnable2: true,
                    biblename: decodeURI(v)
                });
            } else {
                that.setData({
                    newMenu: '经典家常菜',
                    pageEnable1: false,
                    pageEnable2: false,
                });
                wx.setNavigationBarTitle({
                    title: '菜谱合本',
                })
            }
            that.loadCatalog();
        }

        that.loadBibleV(preloadFun);

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})